﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DigitalRune.Windows.Docking;
using md.imi.membranes.objects.alphabet;
using md.imi.membranes.objects.strings;

namespace md.imi.membranes.gui.Editor
{
    public partial class MultisetEditor : DockableForm
    {
        private StringObject mSo;
        /// <summary>
        /// 
        /// </summary>
        public MultisetEditor(String membraneLabel)
        {
            InitializeComponent();
            lblInfo.Text = String.Format("Multiset definition for membrane '[{0}]'", membraneLabel);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MultisetEditor_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///     Load data into form controls
        /// </summary>
        /// <param name="membrane"></param>
        public void LoadData(StringObject so)
        {
            mSo = so;
            lbAlphabetObjects.Items.Clear();
            if (so != null)
            {
                foreach (AlphabetObject ao in so)
                {
                    lbAlphabetObjects.Items.Add(ao);
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AlphabetObjectAction(object sender, EventArgs e)
        {
            if (sender == tsEditalphabetObject)
            {
                if (lbAlphabetObjects.SelectedItem != null)
                {
                    var aoEditor = new AlphabetObjectEditor();
                    aoEditor.LoadData(mSo[lbAlphabetObjects.SelectedIndex]);
                    if (aoEditor.ShowDialog() == DialogResult.OK)
                    {
                        mSo[lbAlphabetObjects.SelectedIndex] = aoEditor.GetData();
                        LoadData(mSo);
                    }
                }

            }
            else if (sender == tsAddAlphabetObject)
            {
                var aoEditor = new AlphabetObjectEditor();
                aoEditor.LoadData(new AlphabetObject());
                if (aoEditor.ShowDialog() == DialogResult.OK)
                {
                    if (mSo == null)
                    {
                        mSo = new StringObject();
                    }

                    mSo.Add(aoEditor.GetData());
                    LoadData(mSo);
                }
            }
            else if (sender == tsRemoveAlphabetObject)
            {
                if (lbAlphabetObjects.SelectedItem != null)
                {
                    mSo.RemoveAt(lbAlphabetObjects.SelectedIndex);
                    lbAlphabetObjects.Items.RemoveAt(lbAlphabetObjects.SelectedIndex);
                }
            }

        }

        internal StringObject GetData()
        {
            mSo.Clear();

            foreach (AlphabetObject ao in lbAlphabetObjects.Items)
            {
                mSo.Add(ao);    
            }

            return mSo;
        }

        private void lbAlphabetObjects_DoubleClick(object sender, EventArgs e)
        {
            AlphabetObjectAction(tsEditalphabetObject, null);


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        public void SetInfoText(string info)
        {
            lblInfo.Text = info;
        }
    }
}
